/*
 * @Author: Setsuna
 * @Email: 601890383@qq.com
 * @FilePath: \src\pages\QuotaManage\InventoryTransfer\components\ImportExcelForm.tsx
 * @Date: 2021-12-08 10:33:07
 * @LastEditTime: 2021-12-08 13:53:35
 */
import React, { Component } from 'react'
import { WrappedFormUtils } from 'antd/lib/form/Form'
import classNames from 'classnames'
import { IUdFormItem, IUdModalContentProps, UdForm, validators } from '@/lib'
import { formUtils } from '@/lib/utils/formUtils'
import { IUdFormProps } from '@/lib/ui/UdForm/UdForm/UdForm'
import { Button, message, Upload } from 'antd'
import './style.scss'
export default class ImportExcelForm extends Component<ImportExcelFormProps, ImportExcelFormState> {
    state: ImportExcelFormState = {
    }
    private form: WrappedFormUtils

    componentDidMount() {
        this.props.getHandler && this.props.getHandler(this.getResult)
    }
    //上传前操作
    private beforeUpload = file => {
        return false;
    }

    render() {
        const items: IUdFormItem[] =
            [
                {
                    label: '文件',
                    id: 'file',
                    rules: [validators.required],
                    decoratorOptions: {
                        valuePropName: 'fileList',
                        getValueFromEvent: function (e) {
                            if (Array.isArray(e)) {
                                return e;
                            }
                            const { fileList = [] } = e
                            if (e.fileList.length > 1) {
                                return e.fileList.slice(-1)
                            }
                            return fileList
                        }
                    },
                    render: <Upload
                        showUploadList={true}
                        accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
                        beforeUpload={file => this.beforeUpload(file)}
                    >
                        <Button type="primary">选择文件</Button>
                    </Upload>
                },
            ]
        return (
            <UdForm
                labelCol={{ span: 4 }} wrapperCol={{ span: 20 }}
                {...this.props}
                items={items}
                className={classNames('ud-modal-form', this.props.className)}
                getForm={(e) => {
                    this.form = e
                    if (this.props.getForm) {
                        this.props.getForm(e)
                    }
                }}
            />
        )
    }

    public getResult = () => {
        return new Promise((resolve, reject) => {
            this.form.validateFieldsAndScroll((errors, values) => {
                if (errors) {
                    reject(errors)
                    return
                }
                let finalValues = formUtils.handleValues(values)
                resolve(finalValues)
            })
        })
    }

}

export interface ImportExcelFormProps extends IUdModalContentProps, Omit<IUdFormProps, 'form'> {

}

interface ImportExcelFormState {
}

